package com.lili.collection.jdbc.config;

import com.lili.collection.core.config.OutputConfig;
import com.lili.collection.core.plugins.OutputPlugin;
import com.lili.collection.core.support.ConfigFieldDef;
import com.lili.collection.jdbc.plugins.JdbcOutputPlugin;
import lombok.Data;

/**
 * Jdbc输出配置
 *
 * @author lili
 * @date 2023/2/13 20:42
 */
@Data
public class JdbcOutputConfig extends HikariPoolConfig implements OutputConfig {

    @ConfigFieldDef(type = ConfigFieldDef.Type.STRING, required = false)
    private String schemaName;

    @ConfigFieldDef(type = ConfigFieldDef.Type.STRING, required = true)
    private String tableName;

    @ConfigFieldDef(type = ConfigFieldDef.Type.STRING, required = false)
    private String inserSql;

    @ConfigFieldDef(type = ConfigFieldDef.Type.STRING, required = false)
    private String updateSql;

    @ConfigFieldDef(type = ConfigFieldDef.Type.STRING, required = false)
    private String deleteSql;

    @ConfigFieldDef(type = ConfigFieldDef.Type.BOOLEAN, required = false)
    private boolean deleteMode=false;

    @ConfigFieldDef(type = ConfigFieldDef.Type.BOOLEAN, required = false)
    private boolean enableTransaction=false;

    public JdbcOutputConfig() {
        this.setReadOnly(false);
    }

    @Override
    public void setEnableTransaction(boolean enableTransaction) {
        this.enableTransaction = enableTransaction;
        //默认改为手动提交
        this.setAutoCommit(!enableTransaction);
    }

    @Override
    public boolean isEnableTransaction() {
        return enableTransaction;
    }

    @Override
    public OutputPlugin getPlugin() {
        return new JdbcOutputPlugin();
    }
}
